﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoolERP_BLL;
using CoolERP_Entities;
using System.Web.UI.HtmlControls;

namespace CoolERP_Web.User
{
    public partial class CS_CPHXC_Query : BasePage
    {
        protected string cpxbh
        {
            get { return this.GetUrlParam("cpxbh") == null ? string.Empty : this.GetUrlParam("cpxbh").ToString(); }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindFirst();
                BindData();
            }
        }

        private void BindFirst()
        {
            this.AspNetPager1.PageSize = GridView1.PageSize;
            if (Request.QueryString["cpxbh"] != null)
            {
                imgAdd.Visible = false;
                btnDel.Visible = false;
                GridView1.Columns[0].Visible = false;
                GridView1.Columns[4].Visible = false;
            }
        }

        private void BindData()
        {
            using (CS_CPMCBLL cBLL = new CS_CPMCBLL())
            {
                int RecordCount = 0;
                List<vCPMC> itemLists = new List<vCPMC>();
                if(!string.IsNullOrEmpty(cpxbh))
                    itemLists = cBLL.GetCPMC(t => t.CPXBH == cpxbh, "CPXBH,BH", this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out RecordCount);
                else
                    itemLists = cBLL.GetCPMC(null, "CPXBH,BH", this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out RecordCount);
                this.AspNetPager1.RecordCount = RecordCount;
                GridView1.DataSource = itemLists;
                GridView1.DataBind();
                this.AspNetPager1.Visible = RecordCount > 0;
                panMsg.Visible = !this.AspNetPager1.Visible;
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }

        protected void btnDel_Click(object sender, ImageClickEventArgs e)
        {
            List<CS_CPMC> items = new List<CS_CPMC>();
            CS_CPMC item = new CS_CPMC();
            try
            {
                using (CS_CPMCBLL cBLL = new CS_CPMCBLL())
                {
                    foreach (GridViewRow container in GridView1.Rows)
                    {
                        HtmlInputCheckBox cBox = (HtmlInputCheckBox)container.FindControl("chkSel");
                        if (cBox != null && cBox.Checked)
                        {
                            string[] str = cBox.Value.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                            item = cBLL.GetEntity(t => t.BH == str[0] && t.CPXBH == str[1]);
                            items.Add(item);
                        }
                    }
                    if (cBLL.Deletes(items))
                        ShowMessage("删除成功");
                }
            }
            catch (Exception ex)
            {
                this.ShowMessage("删除失败\n" + ex.Message);

            }
            finally
            {
                BindData();
            }
        }
    }
}